package collector;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;

/**
 * @author dotekien
 *         Date: Mar 13, 2006
 *         Time: 5:32:50 PM
 */
public class Chain {
    private final int id;
    //    private String standardizeForm;
    private int numberOfStatement;
    private ArrayList<Chunk> chunks;


    public Chain(int lengthOfChunk, int id) {
        this.id = id;
        chunks = new ArrayList<Chunk>();
        numberOfStatement = lengthOfChunk;
    }

    public void addChunk(Chunk chunk) {
        chunks.add(chunk);
        //make the link the chain holding this chunk
        chunk.setChain(this);
    }

    public String getStandardizeForm() {
        assert !chunks.isEmpty();
        Chunk ck = chunks.get(0);
        return ck.getStandardizedText();
        // return standardizeForm;
    }

//    public void setStandardizeForm(String standardizeForm) {
//        this.standardizeForm = standardizeForm;
//    }

    public void gatherStandardizingText() {
        //get the first chunk with file and
//        if (chunks.size() > 0) {
//            Chunk ck = chunks.get(0);
//            standardizeForm = ck.getStandardizedText();
//        }
    }

    public String getText(BufferedReader r, int start, int length) throws IOException {
        for (int i = 0; i < start; i++) {
            r.read();
        }

        char[] a = new char[length];
        r.read(a, 0, length);
        return new String(a).intern();
    }

    public int getNumberOfStatement() {
        return numberOfStatement;
    }

    public void setNumberOfStatement(int numberOfStatement) {
        this.numberOfStatement = numberOfStatement;
    }

    public int getNumberOfChunks() {
        return chunks.size();
    }

    public int getId() {
        return id;
    }

    public ArrayList<Chunk> getChunks() {
        return chunks;
    }

    //just for display in table
    public String toStringForTable() {
        StringBuffer sb = new StringBuffer();
        sb.append("info: ").append(chunks.size()).append(" chunks - ").append(numberOfStatement).append(" statements in one chunk.");
        return sb.toString().intern();
    }

    public String toString() {
        StringBuffer sb = new StringBuffer();
        sb.append(id).append(" [").append(chunks.size()).append(" chunks - ").append(numberOfStatement).append(" statements in one chunk" + "]");
        return sb.toString().intern().intern();
    }

    public String toReport() {
        StringBuffer sb = new StringBuffer();
        sb.append(toString()).append(Configuration.EOL);
        for (Chunk c : chunks) {
            sb.append(c.toString()).append(Configuration.EOL);
        }

        return sb.toString().intern();
    }


    public static final Comparator<Chain> NumOfStateIncrease = new Comparator<Chain>() {
        public int compare(Chain chain, Chain anotherChain) {
            return ((Integer) chain.getNumberOfStatement()).compareTo(anotherChain.getNumberOfStatement());
        }
    };

    public static final Comparator<Chain> NumOfStateDecrease = new Comparator<Chain>() {
        public int compare(Chain chain, Chain anotherChain) {
            return -((Integer) chain.getNumberOfStatement()).compareTo(anotherChain.getNumberOfStatement());
        }
    };

    public static final Comparator<Chain> NumOfChunkIncrease = new Comparator<Chain>() {
        public int compare(Chain chain, Chain anotherChain) {
            return ((Integer) chain.chunks.size()).compareTo(anotherChain.chunks.size());
        }
    };

    public static final Comparator<Chain> NumOfChunkDecrease = new Comparator<Chain>() {
        public int compare(Chain chain, Chain anotherChain) {
            return -((Integer) chain.chunks.size()).compareTo(anotherChain.chunks.size());
        }
    };
}
